#include "stdafx.h"
#include "LocalEndPoint.h"
#include "IActiveObject.h"

namespace RoomFW {

LocalEndPoint::LocalEndPoint(IActiveObject* activeObject, const PortIndex& portIndex) :
	activeObject_(activeObject),
	portIndex_(portIndex),
	contractChecker_(activeObject->getContractChecker(portIndex))
{
}

SendMessageError::Reason LocalEndPoint::acceptMessage(MessageID messageID,
													  IPayloadSP& payload,
													  const char* senderName,
													  IEndPointSP replyEndpoint)
{
	// Check for errors.  Contract violations are asserted as a programming error.
	ASSERT(contractChecker_->validMessage(messageID, payload));
	SendMessageError::Reason retVal = SendMessageError::SMER_NONE;
	if (!contractChecker_->validMessage(messageID, payload))
	{
		retVal = SendMessageError::SMER_CONTRACT_VIOLATION;
	}
	else if (activeObject_->isErrored())
	{
		retVal = SendMessageError::SMER_RECEIVER_ERRORED;
	}
	// If no error, put the message.
	if (SendMessageError::SMER_NONE == retVal)
	{
		RoomMessageSP message(new RoomMessage(portIndex_, messageID, IPayload::ReleaseOwnership(payload), senderName, replyEndpoint));
		activeObject_->put(message);
	}
	return retVal;
}

}
